<?php


class Controller{


    protected $tpl=[];
    protected $tpl_mkd=false;


    public function __construct(){
        if(method_exists($this,"__init")){
            $this->__init();
        }
    }


    public function assign($var=null,$value=null){
        if(is_array($var)){
            $this->tpl = array_merge($this->tpl , $var);
        }else
        $this->tpl[$var]=$value;
    }

    public function display($tpl=null){
        $tpl=$tpl ?? ACTION_NAME;
        if(!strstr($tpl,".")){
            $tpl .= ".html";
        }
        extract($this->tpl);
        $path=APP_VIEW_PATH . "/" . CONTROLLER_NAME . "/" .$tpl;
        if($this->tpl_mkd){
            is_dir(dirname($path))  or mkdir(dirname($path), 0777, true);
            $str=<<<EOF
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<h1>$tpl 模板创建成功！</h1>
</body>
</html>
EOF;

            is_file($path) or file_put_contents($path,$str);
        }
        if(!is_file($path)){
            die($path . "模板不存在 (: !");
        }
        require_once $path;
    }

    public function success($msg=""){
        echo "ok";
    }
}